using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;

namespace LFromeWork
{
    public class GameLaunch : MonoBehaviour
    {
        //全局参数设置
        [FoldoutGroup("ParamsSettings")]
        [SerializeField]
        private ParamsSettings m_ParamsSettings;
        public  static ParamsSettings ParamsSettings { get; private set; }

        [SerializeField, Header("对象池回收parent")]
        public Transform PoolParent;
        
        #region ui 画布基础设置
        [SerializeField,Header("基础Canvas")]
        private Canvas Canvas;
        [SerializeField] 
        public CanvasScaler CanvasScaler;
        [SerializeField,Header("Canvas默认宽")]
        private int DefaultWidth;
        [SerializeField,Header("Canvas默认高")]
        private int DefaultHeight;
        #endregion
        
        #region 管理器 变量

        public static ResourcesManager Resources { get; private set; }
        public static UIManager UI { get; private set; }
        public static CheckVersionManager CheckVersion { get; private set; }
        public static EventManager Event { get; private set; }
        public static PoolManager Pool { get; private set; }
        public static DownloadABManager DownloadAB { get; private set; }
        public static HttpManager Http { get; private set; }
        public static SocketManager Socket { get; private set; }

        #endregion
        
        #region 生命周期函数
        /// <summary>
        /// 单例
        /// </summary>
        public static GameLaunch Instance { get; private set; }
        
        void Awake()
        {
            Instance = this;
            ParamsSettings = m_ParamsSettings;
            CreateManager();
            InitManager();
        }
        
        void Update()
        {
            Resources.OnUpdate();
            DownloadAB.OnUpdate();
        }

        private void OnDestroy()
        {
            ReleaseManager();
        }

        #endregion
        
        #region 创建管理器
        /// <summary>
        /// 创建 管理器
        /// </summary>
        private void CreateManager()
        {
            Resources = new ResourcesManager();
            UI = new UIManager();
            CheckVersion = new CheckVersionManager();
            Event = new EventManager();
            Pool = new PoolManager();
            DownloadAB = new DownloadABManager();
            Http = new HttpManager();
            Socket = new SocketManager();
        }

        /// <summary>
        /// 初始化 Manager
        /// </summary>
        private void InitManager()
        {
            UI.Init(Canvas,CanvasScaler,DefaultWidth,DefaultHeight);
            Socket.Init();
            Pool.Init();
        }
        
        /// <summary>
        /// 释放 Manager
        /// </summary>
        private void ReleaseManager()
        {
            Resources.Dispose();
            UI.Dispose();
            CheckVersion.Dispose();
            Event.Dispose();
            Pool.Dispose();
            DownloadAB.Dispose();
            Http.Dispose();
            Socket.Dispose();
        }
        #endregion
        
        #region Log
        
        public static void Log(object message, params object[] args)
        {
#if DEBUG_LOG
            string value = string.Empty;
            if (args.Length == 0)
            {
                value = message.ToString();
            }
            else
            {
                value = string.Format(message.ToString(), args);
            }
            Debug.Log(value);
#endif
        }

        public static void LogWarning(object message, params object[] args)
        {
#if DEBUG_LOG
            string value = string.Empty;
            if (args.Length == 0)
            {
                value = message.ToString();
            }
            else
            {
                value = string.Format(message.ToString(), args);
            }
            Debug.LogWarning(value);
#endif
        }

        public static void LogError(object message, params object[] args)
        {
#if DEBUG_LOG
            string value = string.Empty;
            if (args.Length == 0)
            {
                value = message.ToString();
            }
            else
            {
                value = string.Format(message.ToString(), args);
            }
            Debug.LogError(value);
#endif
        }
        
        #endregion
    }
}


